import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

/* Layout */
import Layout from '@/layout';
import Empty from '@/layout/empty';

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
    authLink: '/example/list'    填写完整权限路径，如若hidden路由填写其父级列表页路由并填写 authSub
    authSub: Update|Insert|Del   二级路由判断，更新，新增，删除
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
    {
        path: '/login',
        component: () => import('@/views/admin/login/index'),
        hidden: true
    },

    {
        path: '/404',
        component: () => import('@/views/admin/404'),
        hidden: true
    },
    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        hidden: true,
        children: [{
            path: 'dashboard',
            name: 'Dashboard',
            component: () => import('@/views/admin/dashboard/index'),
            meta: { title: '管理系统' }
        }]
    }
];

/**
 * 示例
 * {
        path: '/news',
        component: Layout,
        redirect: '/news/list',
        name: 'News',
        meta: { title: '新闻管理' },
        alwaysShow: true,
        children: [
            {
                path: 'list',
                name: 'NewsList',
                component: () => import('@/views/news/list'),
                meta: { title: '新闻列表', authLink: '/news/list' }
            },
            {
                path: 'update/:id(\\d+)',
                name: 'NewsEdit',
                component: () => import('@/views/news/detail'),
                meta: { title: '新闻编辑', type: 'edit', noCache: true, hidden: true, activeMenu: '/news/list', authLink: '/news/list' },
                hidden: true
            },
            {
                path: 'insert',
                name: 'NewsInsert',
                component: () => import('@/views/news/detail'),
                meta: { title: '新闻新增', type: 'insert', noCache: true, hidden: true, activeMenu: '/news/list', authLink: '/news/list' },
                hidden: true
            }
        ]
    }
 */

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 * {authLink} 权限检查链接
 * {authSub} 权限检查子字段，【Insert】【Update】
 */
export const asyncRoutes = [
    {
        path: '/system',
        component: Layout,
        redirect: '/dashboard',
        name: 'System',
        meta: { title: '系统管理' },
        alwaysShow: true,
        children: [
            {
                path: 'role',
                name: 'Role',
                component: () => import('@/views/admin/system/role/index'),
                meta: { title: '角色管理', authLink: '/system/role' }
            },
            {
                path: 'admin',
                name: 'Admin',
                component: () => import('@/views/admin/system/admin/index'),
                meta: { title: '管理员管理', authLink: '/system/admin' }
            }
        ]
    },
    {
        path: '/content',
        component: Layout,
        redirect: '/content/banner',
        name: 'Content',
        meta: { title: '内容管理' },
        alwaysShow: true,
        children: [
            {
                path: 'Banner',
                name: 'ContentBanner',
                component: () => import('@/views/content/banner'),
                meta: { title: 'Banner', authLink: '/content/banner' }
            },
            {
                path: 'clothes',
                name: 'ContentClothes',
                component: () => import('@/views/content/clothes'),
                meta: { title: '潮流服饰', authLink: '/content/clothes' }
            },
            {
                path: 'product',
                name: 'ContentProduct',
                component: () => import('@/views/content/product'),
                meta: { title: '电竞周边', authLink: '/content/product' }
            },
            {
                path: 'about',
                name: 'ContentAbout',
                component: () => import('@/views/content/about'),
                meta: { title: '关于我们', authLink: '/content/about' }
            },
            {
                path: 'news',
                name: 'ContentNews',
                component: () => import('@/views/content/news/list'),
                meta: { title: '品牌活动', authLink: '/content/news' }
            },
            {
                path: 'news/update/:id(\\d+)',
                name: 'ContentNewsEdit',
                component: () => import('@/views/content/news/detail'),
                meta: { title: '品牌活动编辑', type: 'edit', noCache: true, hidden: true, activeMenu: '/content/news', authLink: '/content/news' },
                hidden: true
            },
            {
                path: 'news/insert',
                name: 'ContentNewsInsert',
                component: () => import('@/views/content/news/detail'),
                meta: { title: '品牌活动新增', type: 'insert', noCache: true, hidden: true, activeMenu: '/content/news', authLink: '/content/news' },
                hidden: true
            },
            {
                path: 'contact',
                name: 'ContentContact',
                component: () => import('@/views/content/contact'),
                meta: { title: '联系我们', authLink: '/content/contact' }
            }
        ]
    },
    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
];

const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
});

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter();
    router.matcher = newRouter.matcher; // reset router
}

export default router;
